﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lapas.Common.Forms;
using WorkDAL;
using Lapas.Common;
using WorkDAL.Users;
using Common;
using WorkDAL.BranchCode;

namespace Users
{
    public partial class UsersEditForm : EditForm
    {
        public UsersEditForm()
        {
            InitializeComponent();
            base.OkButton = btnOK;
            base.CloseButton = btnClose;
        }

        public tbUser user = new tbUser();


        private void BindBranchCode()
        {
            List<tbBranchCode> branchCode = BranchCodeDataAccess.Instance().BranchCodeGetList();
            cboBranchCode.DataSource = branchCode;
            cboBranchCode.DisplayMember = "NameViet";
            cboBranchCode.ValueMember = "branchCode";
            cboBranchCode.SelectedValue = CommonSettings.UserBranchCode;
        }

        protected override void BindData()
        {
            if (base.FormAction != EditFormAction.Insert)
            {
                txtUserName.Text = user.UserName.ToString();
                txtUserFullName.Text = user.UserFullName.ToString();
                txtPassWord.Text = user.Password.ToString();
                cboBranchCode.SelectedValue = user.BranchCode;
            }
        }

        protected override bool DoCreate()
        {
            try
            {
                if (!CheckData()) return false;

                tbUser tbuser = new tbUser();
                tbuser.UserName = txtUserName.Text.Trim();
                tbuser.UserFullName = txtUserFullName.Text.Trim();
                tbuser.Password = Helper.Encrypt(txtPassWord.Text.Trim());
                tbuser.Permisstion = string.Empty;
                tbuser.BranchCode = cboBranchCode.SelectedValue.ToString();

                UserDataAccess.Instance().CreateUser(tbuser);
            }
            catch (Exception ex)
            {
                DisplayMessage.ShowError(ex.Message);
            }

            return true;
        }

        protected override bool DoUpdate()
        {
            try
            {
                if (!CheckData()) return false;

                tbUser tbuser = new tbUser();
                tbuser.UserFullName = txtUserFullName.Text.Trim();
                tbuser.UserName = txtUserName.Text.Trim();
                tbuser.Password = txtPassWord.Text.Trim();
                tbuser.BranchCode = cboBranchCode.SelectedValue.ToString();
                UserDataAccess.Instance().UpdateUser(tbuser);
            }
            catch (Exception ex)
            {
                DisplayMessage.ShowError(ex.Message);
            }

            return true;
        }

        private bool CheckData()
        {
            if (txtUserName.Text.Trim() == string.Empty)
            {
                erp.SetError(txtUserName, "Không được để trống người sd");
                return false;
            }

            if (txtUserFullName.Text.Trim() == string.Empty)
            {
                erp.SetError(txtUserFullName, "Không được để trống tên đầy đủ người sd");
                return false;
            }

            return true;
        }

        private void UsersEditForm_Load(object sender, EventArgs e)
        {
            if (base.FormAction == EditFormAction.Change)
            {
                txtUserName.Enabled = false;
                txtPassWord.Enabled = false;
            }

            BindBranchCode();
        }

    }
}
